package net.yakiboo.adrums;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;

public class Song {
	private List<Integer> sound;
	private List<Double> time;
	
	public Song(){
		this.sound = new LinkedList<Integer>();
		this.time = new LinkedList<Double>();
	}
	
	public void addSound(Drum drum, Double time){
		this.sound.add(drum.getSound());
		this.time.add(time);
	}
	
	public Song(List<Integer> sound, List<Double> time){
		this.sound = sound;
		this.time = time;
	}

	public List<Integer> getSound() {
		return sound;
	}

	public List<Double> getTime() {
		return time;
	}

	public void setSound(List<Integer> sound) {
		this.sound = sound;
	}

	public void setTime(List<Double> time) {
		this.time = time;
	}

	public void play(final Context c) {	
		final List<Double> timex = time;
		new Thread(){
			@Override
			public void run(){
				try{
					MediaPlayer mp = MediaPlayer.create(c, sound.get(0));
					mp.start();
				}catch(Exception e){
					Log.e("MediaPlayer", "No se puede reproducir el sonido. Resource incorrecta");
				}
				for(int i=1; i < sound.size(); i++){
					Log.e("adrums", "timex.get(i-1)=" + timex.get(i-1) + ", timex.get(i)=" + timex.get(i));
					long diff = (long)((timex.get(i) - timex.get(i-1))*1000);
					Log.e("adrums", "diff=" + diff);
					try {
						Thread.sleep(diff);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					try{
						MediaPlayer mpx = MediaPlayer.create(c, sound.get(i));
						mpx.start();
					}catch(Exception e){
						Log.e("MediaPlayer", "No se puede reproducir el sonido. Resource incorrecta");
					}
				}
			}
		}.start();
	}
}
